package com.fatri.lessionsimple

import android.Manifest
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Environment
import android.view.SurfaceHolder
import android.widget.Toast
import com.fatri.lessionsimple.util.FFmpegNative
import com.permissionx.guolindev.PermissionX
import kotlinx.android.synthetic.main.activity_main.*
import java.io.File

class MainActivity : AppCompatActivity() {
    lateinit var fFmpegNative: FFmpegNative

    lateinit var path: String
    lateinit var dstFile: File
    var player = -1L

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        fFmpegNative = FFmpegNative()
        // Example of a call to a native method

        initView()

    }

    private fun initView() {
        tv_version.text = fFmpegNative.version_info()
        dstFile = File(
            Environment.getExternalStorageDirectory().absolutePath + File.separator + "Movies",
            "out.mp4"
        )
        path = dstFile.absolutePath
        tv_path.text = path


        surface_view.holder.addCallback(object : SurfaceHolder.Callback {
            override fun surfaceChanged(
                holder: SurfaceHolder?,
                format: Int,
                width: Int,
                height: Int
            ) {
            }

            override fun surfaceDestroyed(holder: SurfaceHolder?) {
            }

            override fun surfaceCreated(holder: SurfaceHolder) {
                if (player == -1L)
                    player = fFmpegNative.createPlayer(path, holder.surface)
            }

        })


        btn_play.setOnClickListener {
            fFmpegNative.play(player)
        }

        btn_stop.setOnClickListener {
            fFmpegNative.stop(player)
        }

    }



}
